<template>
  <div class="hm-upload">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="value" v-errorimg :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除图片 -->
    <div v-if="value" class="del" @click="del">
      <i class="el-icon-circle-close" />
    </div>
    <!-- 预览图片 -->
    <div v-if="value" class="show-big" @click="show = true">
      <i class="el-icon-zoom-in" />
    </div>
    <el-dialog :visible.sync="show" width="600px" title="预览图片">
      <img v-errorimg style="width: 100%" :src="value" alt="">
    </el-dialog>
    <!-- 进度条显示 -->
    <el-progress v-if="percentageShow" :percentage="percentage" />
  </div>
</template>

<script>
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDoW6loqneWrQwTL9qcSQUmq1JdHpIYnuA',
  SecretKey: 'FMORk1Kf2MeqIj8aCmXk9gbUjzNwdVJH'
})
export default {
  name: 'Hmupload',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      show: false,
      percentage: 0,
      percentageShow: false
    }
  },
  methods: {
    // 上传文件前的回调
    beforeAvatarUpload(file) {
      // 限制文件类型和大小
      const typeArr = ['image/jpeg', 'image/png']
      const typeBol = typeArr.includes(file.type)
      if (!typeBol) {
        this.$message.error('请上传jpeg或者png格式文件')
      }
      const limit1M = file.size / 1024 / 1024 < 1
      if (!limit1M) {
        this.$message.error('请上传小于1M的图片')
      }
      return typeBol && limit1M
    },
    // 自定义上传
    httpRequest(res) {
      // 上传时进度条显示
      this.percentageShow = true
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'xx-80-1310838789',
          Region: 'ap-guangzhou',
          Key: res.file.name,
          StorageClass: 'STANDARD',
          Body: res.file,
          onProgress: (progressData) => {
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.percentageShow = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData)) // 上传文件的进度
          }
        },
        (err, data) => {
          //   this.value = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
          console.log(err, data)
        }
      )
    },
    // 删除图片
    del() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.hm-upload {
  position: relative; // 相对定位
  display: inline-block;
  .del {
    position: absolute; // 绝对定位
    top: 5px;
    right: 5px;
    color: #fff;
    cursor: pointer;
    display: none; //鼠标移入显示
  }
  .show-big {
    position: absolute;
    top: 5px;
    right: 30px;
    cursor: pointer;
    display: none;
    color: #fff;
  }
  &:hover {
    .del,
    .show-big {
      display: inline-block;
    }
  }
}
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
